import facade
import parsel
import requests
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)


class DownList(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.Baseurl = "http://202.115.72.61:9099/"
        self.proxy = {
            "http": "192.168.30.176:8131",
            "https": "192.168.30.176:8131",
        }
        self.sn = requests.Session()
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Cache-Control": "no-cache",
            "Host": "202.115.72.61:9099",
            "Referer": "http://202.115.72.61:9099/",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }

        self.mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", self.logger)

    def home(self):
        """
        先访问首页
        :return:
        """
        BoolResult, errString, r = facade.BaseRequest(self.Baseurl,
                                                      sn=self.sn,
                                                      proxies=self.proxy,
                                                      headers=self.headers,
                                                      timeout=(30, 60))
        if BoolResult:
            return True
        else:
            self.logger.info("首页访问错误")
            return False

    def down_list(self):
        url = "http://202.115.72.61:9099/sublibBook/resultlist?SiteID=1&sublibID=2118"
        BoolResult, errString, r = facade.BaseRequest(url,
                                                      sn=self.sn,
                                                      proxies=self.proxy,
                                                      headers=self.headers,
                                                      timeout=(30, 60))
        if BoolResult:
            select = parsel.Selector(text=r.text)
            li_list = select.xpath('.//ul[@class="vertical_nav catalogBookNav"]/li')
            Listpara = []
            for li_tag in li_list:
                one_class = li_tag.xpath('./a[2]/text()').get()
                li_list2 = li_tag.xpath('./ul/li')
                print("***************")
                print(one_class)
                for li_tag2 in li_list2:
                    two_class = li_tag2.xpath('./a[2]/text()').get()
                    li_list3 = li_tag2.xpath('./ul/li')
                    if not two_class:
                        href = li_tag2.xpath('./a/@href').get()
                        text = li_tag2.xpath('./a/text()').get()
                        Listpara.append((one_class + ";" + text, href))
                    else:
                        print(two_class)

                    for li_tag3 in li_list3:
                        three_class = li_tag3.xpath('./a[2]/text()').get()
                        li_list4 = li_tag3.xpath('./ul/li')
                        if not three_class:
                            href = li_tag3.xpath('./a/@href').get()
                            text = li_tag3.xpath('./a/text()').get()
                            Listpara.append((one_class + ";" + two_class + ";" + text, href))
                        else:
                            print(three_class)
                        for li_tag4 in li_list4:
                            four_class = li_tag4.xpath('./a[2]/text()').get()
                            li_list5 = li_tag4.xpath('./ul/li')
                            if not four_class:
                                href = li_tag4.xpath('./a/@href').get()
                                text = li_tag4.xpath('./a/text()').get()
                                Listpara.append((one_class + ";" + two_class + ";" + three_class + ";" + text, href))
                            else:
                                print(four_class)
                            for li_tag5 in li_list5:
                                five_class_is = li_tag5.xpath('./a[2]/text()').get()
                                if five_class_is:
                                    print("出错")
                                five_class = li_tag5.xpath('./a/text()').get()
                                href = li_tag5.xpath('./a/@href').get()
                                print("five")
                                print(five_class)
                                Listpara.append((
                                                one_class + ";" + two_class + ";" + three_class + ";" + four_class + ";" + five_class,
                                                href))
            print(len(Listpara))
            print(Listpara)
            sql = "replace into sublist (`subject`,url) values (%s,%s)"
            self.mysqlutils.ExeSqlMany(sql, Listpara)
        else:
            self.logger.info(errString)


if __name__ == "__main__":
    down = DownList()
    if down.home():
        down.down_list()
